package info.cs52.fes.action.auth;

import info.cs52.fes.Application;
import info.cs52.fes.FesAction;
import info.cs52.fes.action.SessionUtils;
import info.cs52.fes.dao.UserDAO;
import info.cs52.fes.data.User;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class LoginAction extends FesAction {
		
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		if ("login".equals(req.getParameter("submit"))) {
			User user = UserDAO.findByName(req.getParameter("name"));
			if (user != null) {
				SessionUtils.login(req, user);
				String returnUrl = req.getParameter("returnUrl");
				if (returnUrl == null) {
					returnUrl = Application.getPath();
				}
				resp.sendRedirect(returnUrl);				
			}
		}
	}
}
